
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;

using Swaf;
using Swaf.Container;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

[assembly: WebResource("Syrinx.Gui.AspNet.NitobiCtls.nitobi.tabstrip.js", "application/x-javascript")]
namespace Syrinx.Gui.AspNet.NitobiCtls
{
	[ParseChildren(true, "TabItems")]
	[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class NitobiTabs : System.Web.UI.UserControl, INamingContainer
	{
		protected FlexiMap m_macros = new FlexiMap();
		protected List<TabItem> m_tabItems = new List<TabItem>();

		public List<TabItem> TabItems { get { return m_tabItems; } }

		[Category("Appearance")]
		public string Width { get { return (string)m_macros.get("width"); } set { m_macros.put("width", value); } }
		[Category("Appearance")]
		public string Height { get { return (string)m_macros.get("height"); } set { m_macros.put("height", value); } }
		[Category("Appearance")]
		public string TabAlign { get { return (string)m_macros.get("tabAlign"); } set { m_macros.put("tabAlign", value); } }
		[Category("Appearance")]
		public string TabOverlap { get { return (string)m_macros.get("tabOverlap"); } set { m_macros.put("tabOverlap", value); } }

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			string scriptUrl = Page.ClientScript.GetWebResourceUrl(typeof(NitobiTabs), "Syrinx.Gui.AspNet.NitobiCtls.nitobi.tabstrip.js");
			Page.ClientScript.RegisterClientScriptInclude("NitTabStrip", scriptUrl);
			Page.ClientScript.RegisterStartupScript(typeof(NitobiTabs), "Init",
				string.Format("function setupNitTabs(){{nitobi.loadComponent('{0}');}} nitobi.html.attachEvent(window, 'load', setupNitTabs);", ClientID), true);
		}

		protected override void Render(HtmlTextWriter output)
		{
			output.Write("<ntb:tabstrip id='{0}' width='{1}' height='{2}'><ntb:tabs align='{3}' overlap='{4}'>",
							ClientID, Width, Height, TabAlign, TabOverlap);
			foreach (TabItem tab in TabItems)
			{
				string w = Cmn.IsEmpty(tab.Width) ? "" : tab.Width;
				string conType = !Cmn.IsEmpty(tab.NavigationUrl) ? "containertype='iframe'" : "";
				string source = tab.NavigationUrl;
				if (Cmn.IsEmpty(tab.NavigationUrl))
					source = tab.NamedId;
				output.Write("<ntb:tab label='{0}' source='{1}' width='{2}' {3}></ntb:tab>",
							tab.Name, source, w, conType);
			}
			output.Write("</ntb:tabs></ntb:tabstrip>");
		}
	}

	/// <summary>Maintains the state for each tab in the tab strip and can be created by ASP.NET
	/// when embedding the <csf:TabItem> in the tabstrip control in an ASPX/ASCX.</summary>
	[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
	[ParseChildren(true, "TabItems")]
	public class TabItem
	{
		private int m_tabId = -1;
		private string m_namedId;
		private string m_name;
		private string m_navigationUrl;
		private bool m_selected;
		private bool m_disabled;
		private string m_onClick;
		private string m_toolTip;
		private bool m_serverPostback = false;
		private string m_width;
		protected List<TabItem> m_tabItems = new List<TabItem>();

		public List<TabItem> TabItems { get { return m_tabItems; } }

		public TabItem() : this("", "", false, false, "", "", "","") { }

		public TabItem(String name, String navigationUrl, bool selected, bool disabled, string onClientClick, string namedId, string toolTip, string width)
		{
			m_name = name;
			m_navigationUrl = navigationUrl;
			m_selected = selected;
			m_disabled = disabled;
			m_onClick = onClientClick;
			m_namedId = namedId;
			m_toolTip = toolTip;
			m_width = width;
		}

		public string Name { get { return m_name; } set { m_name = value; } }
		public string NavigationUrl { get { return m_navigationUrl; } set { m_navigationUrl = value; } }
		public bool Disabled { get { return m_disabled; } set { m_disabled = value; } }
		public bool Selected { get { return m_selected; } set { m_selected = value; } }
		public string OnClientClick { get { return m_onClick; } set { m_onClick = value; } }
		public string NamedId { get { return m_namedId; } set { m_namedId = value; } }
		public string Width { get { return m_width; } set { m_width = value; } }
		public string ToolTip { get { return m_toolTip; } set { m_toolTip = value; } }
		public int Id { get { return m_tabId; } set { m_tabId = value; } }
		public bool ServerPostBack { get { return m_serverPostback; } set { m_serverPostback = value; } }

	}
}
